package cn.stronglink.asset.module.rfidState.service;

import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.socket.TextMessage;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.toolkit.IdWorker;
//import com.taobao.api.ApiException;

import cn.stronglink.asset.message.mq.consumer.entity.Reader003Udev;
import cn.stronglink.asset.message.websocket.MessageHandler;
import cn.stronglink.asset.model.Alarm;
import cn.stronglink.asset.model.ItAssetExtPosition;
import cn.stronglink.asset.model.RfidState;
import cn.stronglink.asset.module.alarm.mapper.AlarmMapper;
import cn.stronglink.asset.module.assetmanage.itasset.mapper.ItassetExtPositionMapper;
import cn.stronglink.asset.module.assetmanage.itasset.mapper.ItassetMapper;
import cn.stronglink.asset.module.assetmanage.itasset.vo.ItAssetVo;
import cn.stronglink.asset.module.rfidState.mapper.RfidStateMapper;
import cn.stronglink.asset.module.rfidState.vo.InventoryDiffVo;
import cn.stronglink.suite.core.util.PropertiesUtil;
import cn.stronglink.suite.core.util.SmsSender;

@Service
public class RfidStateService {

	@Autowired
	private MessageHandler messageHandler;
	@Autowired
	private RfidStateMapper rfidStateMapper;
	@Autowired
	private ItassetMapper itassetMapper;
	@Autowired
	private AlarmMapper alarmMapper;
	@Autowired
	private ItassetExtPositionMapper itassetExtPositionMapper;
	public static void main(String[] args) {
		
		List<String> aa = new ArrayList<String>();
		aa.add("1");
		aa.add("2");
		aa.add("3");
		aa.add("4");
		aa.add("5");
		aa.add("6");
		aa.add("8");
		aa.add("9");

		List<String> bb = new ArrayList<String>();
		bb.add("1");
		bb.add("2");
		bb.add("3");
		bb.add("4");
		bb.add("5");
		bb.add("9");
		bb.add("15");
		bb.add("11");	
		
		for(int i=0;i<aa.size();i++){
			String cut =aa.get(i);
			boolean flag =false ;
			for(int j=0;j<bb.size();j++){
				if(bb.get(j).equals(cut)){
					System.out.println("排除剩余中存在的-----------》"+bb.get(j));
					bb.remove(j);
					flag=true;
					break;
				}
			}
			// bb 中不存在的 则是要上架的
			if(!flag){
				System.out.println("需要上架设备-------》"+cut);
			}
		}
		
		//排除剩余的-------即要下架的
		System.out.println("需下架设备------------》"+bb);
	}
	/**
	 * 接收2.4g传过来的数据
	 * @param lable
	 * @param leble24gList
	 */
	public void receiver24gLable(String lable, List<String> leble24gList) {
        if(leble24gList!=null&&leble24gList.size()>0) {
       	 for(String assetLable:leble24gList) {
       		 RfidState data =rfidStateMapper.qryReaderDataByAssetLable(assetLable);
       		 RfidState re=new RfidState(); 
       		 if(data!=null) {
       			 //采集信息表里有该标签但是采集的设备号不是一个就更新成当前的采集的设备号
       			 if(!data.getRackConverCode().equals(lable)) {
           			 re.setRackConverCode(lable);
           			 re.setRfidNo(assetLable);
           			 re.setUpdateTime(new Date());
           			 rfidStateMapper.updateRfidInfo(re);
       			 }
       		 }else {
       			//采集信息表里没有该标签添加一条采集信息
       			 re.setId(IdWorker.getId());
       			 re.setRackConverCode(lable);
       			 re.setRfidNo(assetLable);
       			 re.setCreateTime(new Date());
       			 rfidStateMapper.insertRfidState(re);
       		 }
       	 }
        }
	}
	
	public void sendMessageToAll(String json) {
		messageHandler.sendMessageToAllUsers(new TextMessage(json));
		
	}

	public void updateUyn(String rackCode,List<Reader003Udev> list) {
		ItAssetVo positionVo = itassetMapper.getRoomIdByRackConverCode(rackCode.toUpperCase());
		List<RfidState> inventoryRfidList  = new ArrayList<RfidState>(); //实际盘到的设备
		List<RfidState> exsitRfidList = new ArrayList<RfidState>(); 
		for (int i = 0; i < list.size(); i++) {
			Reader003Udev temp =list.get(i);
			if("00000000".equals(temp.getRfid()))
			     continue;  
			String rfid= temp.getRfid().toUpperCase();
			RfidState data =rfidStateMapper.qryReaderDataByAssetLable(rfid);
			RfidState st = new RfidState();
       		if(data!=null) {
       			st.setRfidNo(rfid);
       			st.setuHeight(temp.getuHeight());
				st.setRackConverCode(rackCode.toUpperCase());
				st.setU(temp.getU());
       			//采集信息表里有该标签但是采集的设备号不是一个就更新成当前的采集的设备号
       			if(!data.getRackConverCode().toUpperCase().equals(rackCode.toUpperCase())) {
    				rfidStateMapper.updateRfidInfo(st);
       			 }
       		}else {
   			    //采集信息表里没有该标签添加一条采集信息
       			st.setId(IdWorker.getId());
       			st.setRackConverCode(rackCode.toUpperCase());
       			st.setRfidNo(rfid);
       			st.setuHeight(temp.getuHeight());
       			st.setU(temp.getU());
       			st.setCreateTime(new Date());
   			    rfidStateMapper.insertRfidState(st);
       		}
       		if(positionVo!=null) {
       		  inventoryRfidList.add(st);
       		}
		}
			
		if(positionVo!=null) {
			Long rackId = positionVo.getRackId(); 
			exsitRfidList = itassetMapper.getServerByRackId(rackId);
			compareUDiffYn(inventoryRfidList,exsitRfidList,positionVo);
		}
	}

	private void compareUDiffYn(List<RfidState> inventoryRfidList, List<RfidState> exsitRfidList, ItAssetVo positionVo) {
        // 不同
		Collection<String> diffentReturn = new LinkedList<String>();
		
		//相同
		Collection<String> asReturn = new LinkedList<String>();
		
		Collection<RfidState> overData = new HashSet<RfidState>();
		Collection<RfidState> lossData = new HashSet<RfidState>();
		Collection<RfidState> differentData = new HashSet<RfidState>();
		
		List<RfidState> max =inventoryRfidList;
		List<RfidState> min =exsitRfidList;
		if(max.size()<min.size()){
			max=exsitRfidList;
			min=inventoryRfidList;
		}
		
	     Map<String,Integer> map = new HashMap<String,Integer>(max.size());
	     for (RfidState object : max) {
	         map.put(object.getRfidNo(), 1);
	     }
	     for (RfidState object : min) {
	         if(map.get(object.getRfidNo())==null){
	        	 diffentReturn.add(object.getRfidNo());
	        	 if(max.size()==exsitRfidList.size()&&exsitRfidList.size()!=inventoryRfidList.size()){
	        		 // 如果max<min 说明 max=bb为台账，  min=aa 为盘点 .此时盘点的不在台账中 为盘盈
	        		 overData.add(object);
	        	 }else{
	        		// 此时说明 max=aa为盘点，     min=bb 为台账    此时台账不在盘点中  为盘亏
	        		 lossData.add(object);
	        	 }
	         }else{
	             map.put(object.getRfidNo(), 2);
	         }
	     }
	     // inventoryRfidList所有
	     Map<String,RfidState> inventoryRfidListmap = new HashMap<String,RfidState>(inventoryRfidList.size());
	     for (RfidState object : inventoryRfidList) {
	    	 inventoryRfidListmap.put(object.getRfidNo(), object);
	     }
	     
	     //exsitRfidList 所有
	     Map<String,RfidState> exsitRfidListmap = new HashMap<String,RfidState>(exsitRfidList.size());
	     for (RfidState object : exsitRfidList) {
	    	 exsitRfidListmap.put(object.getRfidNo(), object);
	     }
	     
	     for (Map.Entry<String, Integer> entry : map.entrySet()) {
	         if(entry.getValue()==1){
	        	 if(max.size()==exsitRfidList.size()&&exsitRfidList.size()!=inventoryRfidList.size()){
	        		 lossData.add(exsitRfidListmap.get(entry.getKey()));
	        	 }else{
	        		 overData.add(inventoryRfidListmap.get(entry.getKey()));
	        	 }
	         }
	         if(entry.getValue()==2){
	        	 asReturn.add(entry.getKey());
	         }
	     }
	     
	     /**
	      * 最后再判断 相同的里边有没有差异 asReturn 
	      */
	     for(Iterator<String> iter = asReturn.iterator(); iter.hasNext();){
	    	 String rfid =(String) iter.next();
	    	 RfidState inventoryRfidListobj =inventoryRfidListmap.get(rfid);
	    	 RfidState exsitRfidListobj =exsitRfidListmap.get(rfid);
	    	 if(!inventoryRfidListobj.getU().toString().equals(exsitRfidListobj.getU().toString())||!inventoryRfidListobj.getRackConverCode().equals(exsitRfidListobj.getRackConverCode())){
	    		 //差异
	    		 differentData.add(inventoryRfidListmap.get(rfid));
	    	 }
	     }
	    
	     Long roomId = positionVo.getRoomId();
		 Long rackId = positionVo.getRackId();
		 List<String> alarmServerIds = new ArrayList<String>();
		 List<Map<String, Object>> upList = new ArrayList<Map<String, Object>>();
		 List<Map<String, Object>> downList = new ArrayList<Map<String, Object>>();
		 Map<String, Object> downMap = new HashMap<String, Object>();
		 downMap.put("type", 2);
		 downMap.put("rackCode", rackId+"");
		 Map<String, Object> upMap = new HashMap<String, Object>();
		 upMap.put("type", 1);
		 upMap.put("rackCode", rackId+"");
		 StringBuffer sb =new StringBuffer();
		 sb.append(positionVo.getRackName()+"的");
		 //插入非法下架告警
		 if(lossData!=null&&lossData.size()>0) {
			 for(RfidState s :lossData){
				String code= s.getRfidNo();
				ItAssetVo vo = itassetMapper.getLedgerU(code);//获取台账信息
				Map<String, Object> temp = new HashMap<String, Object>();
				temp.put("Code", code);
		    	String unityModelPath="Server/standard/standard_1u";
		    	if(s.getuHeight()!=null) {
		    		 int i =s.getuHeight();
		    	     switch(i){
		    	        case 1:
		    	        	unityModelPath="Server/standard/standard_1u";break;
		    	        case 2:
		    	        	unityModelPath="Server/standard/standard_2u";break;
		    	        case 3:
		    	        	unityModelPath="Server/standard/standard_3u";break;
		    	        case 4:
		    	        	unityModelPath="Server/standard/standard_4u";break;
		    	        case 5:
		    	        	unityModelPath="Server/standard/standard_5u";break;
		    	        case 6:
		    	        	unityModelPath="Server/standard/standard_6u";break;
		    	        case 7:
		    	        	unityModelPath="Server/standard/standard_7u";break;
		    	        case 8:
		    	        	unityModelPath="Server/standard/standard_8u";break;
		    	        case 9:
		    	        	unityModelPath="Server/standard/standard_9u";break;
		    	        case 10:
		    	        	unityModelPath="Server/standard/standard_10u";break;
		    	        case 11:
		    	        	unityModelPath="Server/standard/standard_11u";break;
		    	        case 12:
		    	        	unityModelPath="Server/standard/standard_12u";break;
		    	        case 13:
		    	        	unityModelPath="Server/standard/standard_13u";break;
		    	        case 14:
		    	        	unityModelPath="Server/standard/standard_14u";break;
		    	        case 15:
		    	        	unityModelPath="Server/standard/standard_15u";break;
		    	        case 16:
		    	        	unityModelPath="Server/standard/standard_16u";break;
		    	        case 17:
		    	        	unityModelPath="Server/standard/standard_17u";break;
		    	        case 18:
		    	        	unityModelPath="Server/standard/standard_18u";break;
		    	        case 19:
		    	        	unityModelPath="Server/standard/standard_19u";break;
		    	        case 20:
		    	        	unityModelPath="Server/standard/standard_20u";break;
		    	        case 21:
		    	        	unityModelPath="Server/standard/standard_21u";break;
		    	        case 22:
		    	        	unityModelPath="Server/standard/standard_22u";break;
		    	        case 23:
		    	        	unityModelPath="Server/standard/standard_23u";break;
		    	        case 24:
		    	        	unityModelPath="Server/standard/standard_24u";break;
		    	        case 25:
		    	        	unityModelPath="Server/standard/standard_25u";break;
		    	        case 26:
		    	        	unityModelPath="Server/standard/standard_26u";break;
		    	        case 27:
		    	        	unityModelPath="Server/standard/standard_27u";break;
		    	        case 28:
		    	        	unityModelPath="Server/standard/standard_28u";break;
		    	        case 29:
		    	        	unityModelPath="Server/standard/standard_29u";break;
		    	        case 30:
		    	        	unityModelPath="Server/standard/standard_30u";break;
		    	        case 31:
		    	        	unityModelPath="Server/standard/standard_31u";break;
		    	        case 32:
		    	        	unityModelPath="Server/standard/standard_32u";break;
		    	        case 33:
		    	        	unityModelPath="Server/standard/standard_33u";break;
		    	        case 34:
		    	        	unityModelPath="Server/standard/standard_34u";break;
		    	        case 35:
		    	        	unityModelPath="Server/standard/standard_35u";break;
		    	        case 36:
		    	        	unityModelPath="Server/standard/standard_36u";break;
		    	        case 37:
		    	        	unityModelPath="Server/standard/standard_37u";break;
		    	        case 38:
		    	        	unityModelPath="Server/standard/standard_38u";break;
		    	        case 39:
		    	        	unityModelPath="Server/standard/standard_39u";break;
		    	        case 40:
		    	        	unityModelPath="Server/standard/standard_40u";break;
		    	        case 41:
		    	        	unityModelPath="Server/standard/standard_41u";break;
		    	        case 42:
		    	        	unityModelPath="Server/standard/standard_42u";break;
		    	        default:
		    	        	unityModelPath="Server/standard/standard_1u";break;
		    	      }
		    	}		    	
				temp.put("BaseModel", unityModelPath);
				temp.put("OwnerCode", rackId+"");
				Map<String, Object> spaceMap = new HashMap<String, Object>();
				spaceMap.put("OwnerIndex",s.getU());
				temp.put("Space", spaceMap);
				downList.add(temp);
				
				alarmServerIds.add(vo.getId_());
				sb.append(+vo.getStartU()+"U的设备非法下架,");
				String desc ="RFID号:"+vo.getRfidNo()+",U位在："+vo.getStartU()+"U的设备非法下架";
				Alarm a =alarmMapper.getAlarmByRfid(s.getRfidNo(),3L);
				if(a!=null) {
					a.setCreateTime(new Date());
					a.setTypeId(3L);
					alarmMapper.updateAlarm(a);
				}else {
					//插入报警信息	
					this.insertIntoAlarm(3L,s.getRfidNo(),s.getAssetId(),roomId+"",rackId+"",desc);
				}
			 }
		 }
		 if(downList.size()>0) {
			 downMap.put("dataList", downList);
			 String downjson = JSONObject.toJSONString(downMap);
			 messageHandler.sendMessageToAllUsers(new TextMessage(downjson));
		 }
		
		 
		 //插入非法上架告警
		 if(overData!=null&&overData.size()>0) {
			 for(RfidState s :overData){
				sb.append(+s.getU()+"U的设备非法上架,");
				String code= s.getRfidNo();
				alarmServerIds.add(code);
				Map<String, Object> temp = new HashMap<String, Object>();
				temp.put("Code", code);
		    	String unityModelPath="Server/standard/standard_1u";
		    	if(s.getuHeight()!=null) {
		    		 int i =s.getuHeight();
		    	     switch(i){
		    	        case 1:
		    	        	unityModelPath="Server/standard/standard_1u";break;
		    	        case 2:
		    	        	unityModelPath="Server/standard/standard_2u";break;
		    	        case 3:
		    	        	unityModelPath="Server/standard/standard_3u";break;
		    	        case 4:
		    	        	unityModelPath="Server/standard/standard_4u";break;
		    	        case 5:
		    	        	unityModelPath="Server/standard/standard_5u";break;
		    	        case 6:
		    	        	unityModelPath="Server/standard/standard_6u";break;
		    	        case 7:
		    	        	unityModelPath="Server/standard/standard_7u";break;
		    	        case 8:
		    	        	unityModelPath="Server/standard/standard_8u";break;
		    	        case 9:
		    	        	unityModelPath="Server/standard/standard_9u";break;
		    	        case 10:
		    	        	unityModelPath="Server/standard/standard_10u";break;
		    	        case 11:
		    	        	unityModelPath="Server/standard/standard_11u";break;
		    	        case 12:
		    	        	unityModelPath="Server/standard/standard_12u";break;
		    	        case 13:
		    	        	unityModelPath="Server/standard/standard_13u";break;
		    	        case 14:
		    	        	unityModelPath="Server/standard/standard_14u";break;
		    	        case 15:
		    	        	unityModelPath="Server/standard/standard_15u";break;
		    	        case 16:
		    	        	unityModelPath="Server/standard/standard_16u";break;
		    	        case 17:
		    	        	unityModelPath="Server/standard/standard_17u";break;
		    	        case 18:
		    	        	unityModelPath="Server/standard/standard_18u";break;
		    	        case 19:
		    	        	unityModelPath="Server/standard/standard_19u";break;
		    	        case 20:
		    	        	unityModelPath="Server/standard/standard_20u";break;
		    	        case 21:
		    	        	unityModelPath="Server/standard/standard_21u";break;
		    	        case 22:
		    	        	unityModelPath="Server/standard/standard_22u";break;
		    	        case 23:
		    	        	unityModelPath="Server/standard/standard_23u";break;
		    	        case 24:
		    	        	unityModelPath="Server/standard/standard_24u";break;
		    	        case 25:
		    	        	unityModelPath="Server/standard/standard_25u";break;
		    	        case 26:
		    	        	unityModelPath="Server/standard/standard_26u";break;
		    	        case 27:
		    	        	unityModelPath="Server/standard/standard_27u";break;
		    	        case 28:
		    	        	unityModelPath="Server/standard/standard_28u";break;
		    	        case 29:
		    	        	unityModelPath="Server/standard/standard_29u";break;
		    	        case 30:
		    	        	unityModelPath="Server/standard/standard_30u";break;
		    	        case 31:
		    	        	unityModelPath="Server/standard/standard_31u";break;
		    	        case 32:
		    	        	unityModelPath="Server/standard/standard_32u";break;
		    	        case 33:
		    	        	unityModelPath="Server/standard/standard_33u";break;
		    	        case 34:
		    	        	unityModelPath="Server/standard/standard_34u";break;
		    	        case 35:
		    	        	unityModelPath="Server/standard/standard_35u";break;
		    	        case 36:
		    	        	unityModelPath="Server/standard/standard_36u";break;
		    	        case 37:
		    	        	unityModelPath="Server/standard/standard_37u";break;
		    	        case 38:
		    	        	unityModelPath="Server/standard/standard_38u";break;
		    	        case 39:
		    	        	unityModelPath="Server/standard/standard_39u";break;
		    	        case 40:
		    	        	unityModelPath="Server/standard/standard_40u";break;
		    	        case 41:
		    	        	unityModelPath="Server/standard/standard_41u";break;
		    	        case 42:
		    	        	unityModelPath="Server/standard/standard_42u";break;
		    	        default:
		    	        	unityModelPath="Server/standard/standard_1u";break;
		    	      }
		    	}
		    	
				temp.put("BaseModel", unityModelPath);
				temp.put("OwnerCode", rackId+"");
				Map<String, Object> spaceMap = new HashMap<String, Object>();
				spaceMap.put("OwnerIndex",s.getU());
				temp.put("Space", spaceMap);
				upList.add(temp);
				
				String desc ="RFID号:"+s.getRfidNo()+",U位在："+s.getU()+"U的设备非法上架";
				//插入报警信息	
				Alarm a =alarmMapper.getAlarmByRfid(s.getRfidNo(),2L);
				if(a!=null) {
					a.setTypeId(2L);
					a.setCreateTime(new Date());
					alarmMapper.updateAlarm(a);
				}else {
					//插入报警信息	
					this.insertIntoAlarm(2L,s.getRfidNo(),null,roomId+"",rackId+"",desc);
				}
			 }
		 }
		 
		 upMap.put("dataList", upList);
		 String upjson = JSONObject.toJSONString(upMap);
		 messageHandler.sendMessageToAllUsers(new TextMessage(upjson));
		 
		 
		//位置差异告警
		 if(differentData!=null&&differentData.size()>0){
			 for(RfidState rs :differentData){
				ItAssetVo vo = itassetMapper.getLedgerU(rs.getRfidNo());//获取台账信息
				alarmServerIds.add(vo.getId_());
				sb.append(+vo.getStartU()+"U的设备位置差异, ");
				String desc ="RFID号:"+vo.getRfidNo()+",U位异常，台账U位："+vo.getStartU()+"U,实际U位："+rs.getU()+"U";
				Alarm a =alarmMapper.getAlarmByRfid(rs.getRfidNo(),1L);
				if(a!=null) {
					a.setTypeId(1L);
					a.setCreateTime(new Date());
					alarmMapper.updateAlarm(a);
				}else {
					//插入报警信息	
					this.insertIntoAlarm(1L,rs.getRfidNo(),rs.getAssetId(),roomId+"",rackId+"",desc);
				}
		   }
		 }
		 if(alarmServerIds!=null&&alarmServerIds.size()>0) {
			Map<String,Object> result = new HashMap<>();	
			result.put("alarmServerIds", alarmServerIds);
			result.put("roomCode", positionVo.getRoomCode());
			result.put("rackCode", rackId+"");
			result.put("alarmCount", 0);
			System.out.println(result+"^^^^^^^^^^^^^^^^result…………………………………………………………");		
			String json = JSONObject.toJSONString(result);
			messageHandler.sendMessageToAllUsers(new TextMessage(json));
		 }
		 if((alarmServerIds!=null&&alarmServerIds.size()>0)||(overData!=null&&overData.size()>0)) {
			 String jsonDesc="{\"tag\":\":当前有设备位置发生变化\",\"desc\":\":位置差异\"}";  
			 System.out.println("^^^^^^^^^^^^^^^^发送短信…………………………………………………………"+jsonDesc);	
			 String phone = PropertiesUtil.getString("yn_phone");
			 //SmsSender.send("思创众联", jsonDesc, phone, "SMS_118655103");  
		 }
	}

	private void insertIntoAlarm(Long typeId, String rfidNo, String assetId, String roomId, String rackId,String desc) {                   
		Alarm alarm = new Alarm();
		alarm.setId(IdWorker.getId());
		alarm.setTypeId(typeId);
		alarm.setName(rfidNo);
		alarm.setLevel("重要");
		alarm.setAssetId(assetId);
		alarm.setRoomId(roomId);
		alarm.setRackId(rackId);
		alarm.setDesc(desc);
		alarm.setCreateTime(new Date());                                                                                                                               
		alarm.setStatus(1);
		alarmMapper.insertAlarm(alarm);
		
	}


	public void sendDataToUnicom(String cabinetCode, List<Reader003Udev> list) throws Exception {		
		System.out.println(cabinetCode+"机柜号&&&&");		
		cabinetCode=cabinetCode.toUpperCase();
		ItAssetVo positionVo = itassetMapper.getRoomIdByRackConverCode(cabinetCode);
		if(positionVo!=null){		
			Long rackId = positionVo.getRackId();
			rfidStateMapper.deleteByRackCode(cabinetCode);
			List<RfidState> inventoryRfidList  = new ArrayList<RfidState>(); //实际盘到的设备
			List<RfidState> exsitRfidList = itassetMapper.getServerByRackId(rackId);
			if(list!=null&&list.size()>0){
			  for (int i = 0; i < list.size(); i++) {
				Reader003Udev temp =list.get(i);
				//更新t_rfid_state表里的U位信息		
				if("00000000".equals(temp.getRfid()))
				     continue;  
				String rfid=temp.getRfid().toUpperCase();
				RfidState data =rfidStateMapper.qryReaderDataByAssetLable(rfid);
				RfidState st = new RfidState();
	       		 if(data!=null) {
	       			 //采集信息表里有该标签但是采集的设备号不是一个就更新成当前的采集的设备号
	       			 if(!data.getRackConverCode().toUpperCase().equals(cabinetCode)) {
	       				st.setRfidNo(rfid);
	    				st.setRackConverCode(cabinetCode);
	    				st.setU(temp.getU());
	    				rfidStateMapper.updateRfidInfo(st);
	       			 }
	       		 }else {
	       			  //采集信息表里没有该标签添加一条采集信息
		       			st.setId(IdWorker.getId());
		       			st.setRackConverCode(cabinetCode);
		       			st.setRfidNo(rfid);
		       			st.setU(temp.getU());
		       			st.setCreateTime(new Date());
	       			    rfidStateMapper.insertRfidState(st);
	       		 }
				
				inventoryRfidList.add(st);

			  }
			  System.out.println(JSON.toJSONString(inventoryRfidList)+"inventoryRfidList&&&&");
			  System.out.println(JSON.toJSONString(exsitRfidList)+"exsitRfidList&&&&");
			  compareDiff(inventoryRfidList,exsitRfidList,positionVo);
			
		  }
			
		}
	}

	private void compareDiff(List<RfidState> inventoryRfidList, List<RfidState> exsitRfidList,ItAssetVo positionVo) throws Exception {
        // 不同
		Collection<String> diffentReturn = new LinkedList<String>();
		
		//相同
		Collection<String> asReturn = new LinkedList<String>();
		
		Collection<RfidState> overData = new HashSet<RfidState>();
		Collection<RfidState> lossData = new HashSet<RfidState>();
		Collection<RfidState> differentData = new HashSet<RfidState>();
		
		List<RfidState> max =inventoryRfidList;
		List<RfidState> min =exsitRfidList;
		if(max.size()<min.size()){
			max=exsitRfidList;
			min=inventoryRfidList;
		}
		
	     Map<String,Integer> map = new HashMap<String,Integer>(max.size());
	     for (RfidState object : max) {
	         map.put(object.getRfidNo(), 1);
	     }
	     for (RfidState object : min) {
	         if(map.get(object.getRfidNo())==null){
	        	 diffentReturn.add(object.getRfidNo());
	        	 if(max.size()==exsitRfidList.size()&&exsitRfidList.size()!=inventoryRfidList.size()){
	        		 // 如果max<min 说明 max=bb为台账，  min=aa 为盘点 .此时盘点的不在台账中 为盘盈
	        		 overData.add(object);
	        	 }else{
	        		// 此时说明 max=aa为盘点，     min=bb 为台账    此时台账不在盘点中  为盘亏
	        		 lossData.add(object);
	        	 }
	         }else{
	             map.put(object.getRfidNo(), 2);
	         }
	     }
	     // inventoryRfidList所有
	     Map<String,RfidState> inventoryRfidListmap = new HashMap<String,RfidState>(inventoryRfidList.size());
	     for (RfidState object : inventoryRfidList) {
	    	 inventoryRfidListmap.put(object.getRfidNo(), object);
	     }
	     
	     //exsitRfidList 所有
	     Map<String,RfidState> exsitRfidListmap = new HashMap<String,RfidState>(exsitRfidList.size());
	     for (RfidState object : exsitRfidList) {
	    	 exsitRfidListmap.put(object.getRfidNo(), object);
	     }
	     
	     for (Map.Entry<String, Integer> entry : map.entrySet()) {
	         if(entry.getValue()==1){
	        	 if(max.size()==exsitRfidList.size()&&exsitRfidList.size()!=inventoryRfidList.size()){
	        		// 如果max<min 说明 max=bb为台账，  min=aa 为盘点
		        	// 此时entry.getvalue==1的 说明     盘点中没有的此时为盘亏
	        		 lossData.add(exsitRfidListmap.get(entry.getKey()));
	        	 }else{
	        		// 此时说明 max=aa为盘点，     min=bb 为台账
		        	// 此时entry.getvalue==1的 说明     台账中没有的 此时为盘盈
	        		 overData.add(inventoryRfidListmap.get(entry.getKey()));
	        	 }
	         }
	         if(entry.getValue()==2){
	        	 asReturn.add(entry.getKey());
	         }
	     }
	     
	     /**
	      * 最后再判断 相同的里边有没有差异 asReturn 
	      */
	     
	     for(Iterator<String> iter = asReturn.iterator(); iter.hasNext();){
	    	 String rfid =(String) iter.next();
	    	 RfidState inventoryRfidListobj =inventoryRfidListmap.get(rfid);
	    	 RfidState exsitRfidListobj =exsitRfidListmap.get(rfid);
	    	 
	    	 if(!inventoryRfidListobj.getU().toString().equals(exsitRfidListobj.getU().toString())||!inventoryRfidListobj.getRackConverCode().equals(exsitRfidListobj.getRackConverCode())){
	    		 //差异
	    		 differentData.add(inventoryRfidListmap.get(rfid));
	    		 
	    	 }
	    	 
	     }
	     Long roomId = positionVo.getRoomId();
		 String roomName = positionVo.getRoomName();
		 Long rackId = positionVo.getRackId();
		 String rackName = positionVo.getRackName();
		 List<InventoryDiffVo> diffentDataIn = new ArrayList<InventoryDiffVo>();
		 List<ItAssetExtPosition> diffentPData = new ArrayList<ItAssetExtPosition>();
		 //更新盘亏
		 if(lossData!=null&&lossData.size()>0) {
			 for(RfidState s :lossData){
				ItAssetVo vo = itassetMapper.getLedgerU(s.getRfidNo());//获取台账信息
				InventoryDiffVo diff = new InventoryDiffVo();
				diff.setCmdbId(vo.getId());
				diff.setRfId(s.getRfidNo());
			    diff.setRackName(rackName);
				diff.setRoomName(roomName);
				diff.setStartu(s.getU());
				diff.setType("下架");
				diffentDataIn.add(diff);		
			 }
			
		 }
		 
		 //更新差异
		 if(differentData!=null&&differentData.size()>0){
			 for(RfidState rs :differentData){
				ItAssetVo vo = itassetMapper.getLedgerU(rs.getRfidNo());//获取台账信息
				InventoryDiffVo diff = new InventoryDiffVo();
				diff.setCmdbId(vo.getId());
				diff.setRfId(rs.getRfidNo());
				diff.setRackName(rackName);
				diff.setRoomName(roomName);
				diff.setStartu(rs.getU());
				diff.setType("上架");
				diffentDataIn.add(diff);		
				//更新库里设备位置信息
				Integer uHeight = 1;
				if(!"".equals(vo.getuHeight())&&vo.getuHeight()!=null){
					uHeight = vo.getuHeight();
				}
				
				ItAssetExtPosition extPosition = new ItAssetExtPosition();
				extPosition.setAssetId(vo.getId());
				extPosition.setRoomId(roomId);
				extPosition.setRackId(rackId);
				extPosition.setStart(rs.getU());
				extPosition.setEnd(rs.getU()+uHeight-1);
				extPosition.setUpdateTime(new Date());
				diffentPData.add(extPosition);
				
		   }
		 }
		    System.out.println(JSON.toJSONString(diffentDataIn)+"传给uop的非法上下架数据&&&&");
			if(diffentDataIn!=null&&diffentDataIn.size()>0){
				//发送请求
				String json = JSON.toJSONString(diffentDataIn);
				System.out.println(JSON.toJSON(json)+"传给uop的非法上下架数据&&&&11");
				CloseableHttpClient httpclient = HttpClients.createDefault();
				HttpUriRequest sync = RequestBuilder.post()  
						.setUri(new URI(PropertiesUtil.getString("visitUrl")+"updateResPosition"))
						.addHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8")
						.addParameter("value", json)	
						.build();
				    CloseableHttpResponse response = httpclient.execute(sync);
					HttpEntity result = response.getEntity();
				try {
						String msg = EntityUtils.toString(result);
						System.out.println(msg+"--------------------非法上下架数据结束---------------------------------");
							
					    if(diffentPData!=null&&diffentPData.size()>0){
							itassetExtPositionMapper.updateItAssetExtPositionBatch(diffentPData);
						}
					
				} finally {
					System.out.println("2222");
					response.close();
					httpclient.close();
				}
			}
		
	}
}
